package com.aries.ui.widget.demo.util;

import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;

/**
 * @Author: AriesHoo on 2019/4/12 11:19
 * @E-Mail: AriesHoo@126.com
 * @Function: 尺寸转换工具
 * @Description:
 */
public class SizeUtil {

    private static Display display;

    public static void getDisplayMetrics(Context context) {
        display = DisplayManager.getInstance().getDefaultDisplay(context).get();
    }

    public static int getScreenWidth() {
        return display.getAttributes().width;
    }

    public static int getScreenHeight() {
        return display.getAttributes().height;
    }

    public static int px2dp(float pxValue) {
        final float scale = display.getAttributes().densityPixels;
        return (int) ((double)pxValue / (double)scale + 0.5d);
    }

    public static int dp2px(float dpValue) {
        final float scale = display.getAttributes().densityPixels;
        return (int) ((double)dpValue * (double)scale + 0.5d);
    }

    public static int px2sp(float pxValue) {
        final float fontScale = display.getAttributes().scalDensity;
        return (int) ((double)pxValue /(double) fontScale + 0.5d);
    }

    public static int sp2px(float spValue) {
        final float fontScale = display.getAttributes().scalDensity;
        return (int) ((double)spValue * (double)fontScale + 0.5d);
    }
}
